//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.group;

import java.io.Serializable;
import java.util.Collection;
import org.jpgolay.wi.persistence.ObjectCRUD;

// Referenced classes of package org.jpgolay.wi.group:
//            Group
public class GroupCRUD
        implements Serializable {

    public GroupCRUD() {
        crud = new ObjectCRUD();
    }

    public boolean find(String id) {
        data = readGroup(new Integer(id));
        return data != null;
    }

    public Collection findAll() {
        return readAllGroup();
    }

    public boolean modify() {
        return updateGroup();
    }

    public boolean create() {
        Integer id = createGroup();
        if (id != null) {
            data = readGroup(id);
            return true;
        } else {
            return false;
        }
    }

    public boolean remove() {
        return deleteGroup();
    }

    public String getId() {
        return data.getId().toString();
    }

    public String getName() {
        return data.getName();
    }

    public void setName(String name) {
        data.setName(name);
    }

    public String getRole() {
        return data.getRole();
    }

    public void setRole(String role) {
        data.setRole(role);
    }

    public String getNote() {
        return data.getNote();
    }

    public void setNote(String note) {
        data.setNote(note);
    }

    public Group getData() {
        return data;
    }

    public void setData(Group group) {
        data = group;
    }

    private Integer createGroup() {
        return crud.create(6);
    }

    private Group readGroup(Integer id) {
        return (Group) crud.read(id, 6);
    }

    private Collection readAllGroup() {
        return crud.readAll(null, 6);
    }

    private boolean updateGroup() {
        return crud.update(data, 6);
    }

    private boolean deleteGroup() {
        return crud.delete(data.getId(), data.getLastUpdate(), 6);
    }
    private Group data;
    private ObjectCRUD crud;
}
